/**
 * JudgePaperController.java
 * Version 1.0.0
 * Created on 2018年7月19日
 *
 */
package com.yincheng.oes.business.exam.judgepaper.controller;

import com.yincheng.oes.business.config.PaperResultStatus;
import com.yincheng.oes.business.exam.judgepaper.entity.PaperResult;
import com.yincheng.oes.business.exam.judgepaper.entity.PaperStudentResult;
import com.yincheng.oes.business.exam.judgepaper.service.PaperResultService;
import com.yincheng.oes.business.util.dictionary.DictionaryService;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import static com.yincheng.oes.business.util.PaperUtils.getAnswersList;

/**
 * @author yangsy
 * @version 1.0.0
 * @since
 * 创建时间：2018年7月19日
 * <p>
 * 功能描述：评判试卷控制层
 * <p>
 * 邮箱地址：446948434@qq.com
 */
@Controller
@RequestMapping("/judgePaper")
public class JudgePaperController {

    @Autowired
    private PaperResultService paperResultService;

    @Autowired
    private DictionaryService dictionaryService;

    /**
     * 答卷页面（编辑）
     *
     * @param view
     * @param resultId
     * @return
     */
    @RequestMapping(value = "/editPage.html", method = RequestMethod.GET)
    public ModelAndView editPage(ModelAndView view, String resultId) {
        PaperStudentResult paperResult = paperResultService.getPaperResult(resultId);
        view.addObject("form", paperResult);

        setView(view);

        view.setViewName("/business/mygrade/answer-edit");
        return view;
    }

    /**
     * 根据试卷id获取当前用户的答卷详细信息
     *
     * @param view
     * @param paperId
     * @return
     */
    @RequestMapping(value = "/draftPage.html", method = RequestMethod.GET)
    public ModelAndView draftPage(ModelAndView view, String paperId) {
        PaperStudentResult paperResult = paperResultService.getPaperResultByPaperId(paperId);
        view.addObject("form", paperResult);
        setView(view);

        view.setViewName("/business/mygrade/answer-edit");
        return view;
    }

    /**
     * 答卷页面（只读）
     *
     * @param view
     * @param resultId
     * @return
     */
    @RequestMapping(value = "/readPage.html", method = RequestMethod.GET)
    public ModelAndView readPage(ModelAndView view, String resultId) {
        PaperStudentResult paperResult = paperResultService.getPaperResult(resultId);
        view.addObject("form", paperResult);

        setView(view);

        view.setViewName("/business/mygrade/answer-read");
        return view;
    }

    /**
     * 完成答题的只读页面
     *
     * @param view
     * @param resultId
     * @return
     */
    @RequestMapping(value = "/donePage.html", method = RequestMethod.GET)
    public ModelAndView donePage(ModelAndView view, String resultId) {
        PaperStudentResult paperResult = paperResultService.getPaperResult(resultId);
        view.addObject("form", paperResult);

        setView(view);

        view.setViewName("/business/mygrade/answer-done");
        return view;
    }

    /**
     * @param view
     */
    private void setView(ModelAndView view) {
        List<String> answers = getAnswersList();
        view.addObject("answers", answers);

        view.addObject("questypes", dictionaryService.getByModel("ques_type"));
    }

    /**
     * 打开并跳转到相应的答卷页面
     *
     * @param view
     * @param resultId
     * @return
     */
    @RequestMapping(value = "/openAndJumpPaperPage", method = RequestMethod.GET)
    public ModelAndView openAndJumpPaperPage(ModelAndView view, String resultId) {
        view.addObject("resultId", resultId);
        PaperResult paperResult = paperResultService.getPaperResultBaseInfo(resultId);
        if ("3".equals(paperResult.getStatus())) {
            view.setViewName("redirect:/judgePaper/readPage.html");
        } else {
            view.setViewName("redirect:/judgePaper/editPage.html");
        }
        return view;
    }

    /**
     * 保存学生和试卷的关联关系
     *
     * @param resultId
     * @param paperId
     * @param userId
     */
    @RequestMapping(value = "/saveStudentOfPaper.json", method = RequestMethod.POST)
    public void saveStudentOfPaper(String resultId, String paperId, String userId) {
        paperResultService.saveUserOfPaper(resultId, paperId, userId);
    }

    /**
     * 保存学生考试成绩
     *
     * @param bean
     */
    @RequestMapping(value = "/savePaperResult.json", method = RequestMethod.POST)
    public void savePaperResult(PaperResult bean) {
        paperResultService.savePaperResult(bean);
    }

    /**
     * 提交学生成绩
     *
     * @param bean
     */
    @RequestMapping(value = "/submitPaperResult.json", method = RequestMethod.POST)
    public void submitPaperResult(PaperResult bean) {
        bean.setStatus(PaperResultStatus.END_ANSWER.getStatus());// 回答完毕
        paperResultService.savePaperResult(bean);
    }

    @InitBinder // 类初始化是调用的方法注解
    public void initBinder(WebDataBinder binder) {
        // 给这个controller配置接收list的长度1024，仅在这个controller有效
        binder.setAutoGrowCollectionLimit(1024);
    }
}
